using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Dts.Runtime;

namespace SsisUtilities.PacMan.Components
{
    public class SsisHelper
    {
        public static bool IsCastValid(TypeCode dataType, object value)
        {
            // from http://msdn2.microsoft.com/en-us/library/ms136082.aspx
            // The DataType of a variable is not set explicitly, but is 
            // inferred from the initial value assigned to the variable, 
            // and cannot be changed afterward. 

            try
            {
                switch (dataType)
                {
                    case TypeCode.Boolean:
                        bool boolTest = bool.Parse(value.ToString());
                        break;
                    case TypeCode.Byte:
                        byte byteTest = byte.Parse(value.ToString());
                        break;
                    case TypeCode.Char:
                        char charTest = char.Parse(value.ToString());
                        break;
                    case TypeCode.DateTime:
                        DateTime dateTimeTest = DateTime.Parse(value.ToString());
                        break;
                    case TypeCode.Decimal:
                        decimal decimalTest = decimal.Parse(value.ToString());
                        break;
                    case TypeCode.Double:
                        double doubleTest = double.Parse(value.ToString());
                        break;
                    case TypeCode.Int16:
                        Int16 int16Test = Int16.Parse(value.ToString());
                        break;
                    case TypeCode.Int32:
                        Int32 int32Test = Int32.Parse(value.ToString());
                        break;
                    case TypeCode.Int64:
                        Int64 int64Test = Int64.Parse(value.ToString());
                        break;
                    case TypeCode.Object:
                        // this test always succeeds
                        break;
                    case TypeCode.SByte:
                        sbyte sbyteTest = sbyte.Parse(value.ToString());
                        break;
                    case TypeCode.Single:
                        Single singleTest = Single.Parse(value.ToString());
                        break;
                    case TypeCode.String:
                        string stringTest = value.ToString();
                        break;
                    case TypeCode.UInt16:
                        UInt16 uInt16Test = UInt16.Parse(value.ToString());
                        break;
                    case TypeCode.UInt32:
                        UInt32 uInt32test = UInt32.Parse(value.ToString());
                        break;
                    case TypeCode.UInt64:
                        UInt64 uInt64Test = UInt64.Parse(value.ToString());
                        break;
                    default:
                        // this should just leave DbNull and Empty
                        throw new ApplicationException("Unsupported data type specified.");
                }
                return true;
            }
            catch
            {
                return false;
            }

        }

        #region GetQualifiedVariableName Overloads

        public static string GetQualifiedVariableName(string variableName,
            string variableNamespace)
        {
            return variableNamespace + "::" + variableName;
        }

        public static string GetQualifiedVariableName(Variable variable)
        {
            return variable.Namespace + "::" + variable.Name;
        }

        #endregion

        #region GetVariableExpression Overloads

        public static string GetVariableExpression(Variable variable)
        {
            return string.Format("@[{0}]",
                GetQualifiedVariableName(variable));
        }

        public static string GetVariableExpression(string qualifiedVariableName)
        {
            return string.Format("@[{0}]",
                qualifiedVariableName);
        }


        public static string GetVariableExpression(string variableName,
            string variableNamespace)
        {
            return string.Format("@[{0}]",
                GetQualifiedVariableName(variableName, variableNamespace));
        }

        #endregion

    }
}
